<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Menumodel extends Model {
	
	var $mid = null;
	var $name = null;
	var $title = null;
	var $access_groups = null;
	var $comment = null;
	var $style = null;
	var $items = array();

	
	function __construct ()
	{
		parent::Model();
		$this->load->database();
	}
	
	
	function get () {
		
		$this->db->select('m.*');
		$this->db->from('menu AS `m`');
		$this->db->join('access_group_menus AS `agm`', 'agm.menu_id=m.mid', 'INNER');
		$this->db->join('accessgroups AS `ag`', 'ag.id=agm.group_id', 'INNER');
		$this->db->join('access_group_user AS `agu`', 'ag.id=agu.group_id', 'INNER');
		
		if (is_int($this->mid)) { $this->db->where('mid', $this->mid); }
		else { $this->db->where('m.name', $this->mid); }
		
		$this->db->where('agu.user_id', (int)$this->session->userdata('user_id'));
		
		$res = $this->db->get()->result();
		
		if (count($res)) { return $res[0]; }
		else { return false; }
	}
	
	function getMenuItems($menu_id) {

		$this->db->select('menuitems.*');
		$this->db->from('menuitems');
		$this->db->join('access_group_menuitems', 'access_group_menuitems.menuitem_id=menuitems.miid', 'INNER');
		$this->db->join('accessgroups', 'accessgroups.id=access_group_menuitems.group_id', 'INNER');
		$this->db->join('access_group_user', 'accessgroups.id=access_group_user.group_id', 'INNER');
		$this->db->groupby('menuitems.miid');
		
		$this->db->where('menu_id', $mid);
		$this->db->where('access_group_user.user_id', (int)$this->session->userdata('user_id'));
		
		$result = $this->db->get()->result();
		
		$this->db->select('menuitems.*');
		$this->db->from('menuitems');
		$this->db->join('accesscustom', 'accesscustom.object_id=menuitems.miid', 'INNER');
		$this->db->where('menu_id', $mid);
		$this->db->where('accesscustom.user_id', (int)$this->session->userdata('user_id'));
		$this->db->where('accesscustom.object_type', 'menuitem');
		
		$resultCustom = $this->db->get()->result();
		
	}

	function save () {
		$save = array(
			'name' => $this->name,
			'title' => $this->title,
			'comment' => $this->comment,
			'style' => $this->style);
		
		if ($this->mid) {
			$this->where("mid", $this->id);
			$this->db->update("menu", $save);
		} else {
			$this->db->insert("menu", $save);
			$this->mid = $this->db->insert_id();
		}
		
		foreach ($this->access_groups as $key => $group) { 
			$saveAccess = array(
				'menu_id' => $this->mid,
				'group_id' => $group_id);
			$this->db->insert('access_group_menus', $saveAccess);
		}
		
		if (count($this->items)) {
			foreach ($this->items as $key => $item) {
				$save = array(
					'menu_id' => $this->mid,
					'title' => $item->title,
					'image' => $item->image,
					'style' => $item->style,
					'path' => $item->path);
				if ($item->miid) {
					$this->where("miid", $item->miid);
					$this->db->update("menuitems", $save);
				} else {
					$this->db->insert("menuitems", $save);
					$item->miid = $this->db->insert_id();
				}
				
				if (!count($item->access_groups)) { $item->access_groups = $this->access_groups; }
				
				foreach ($item->access_groups as $key => $group) {
					$saveAccess = array(
						'menuitem_id' => $item->miid,
						'group_id' => $group_id);
					$this->db->insert('access_group_menuitems', $saveAccess);
				}
			}
		}
	}
	
	function delete() {
		if ($this->mid) {
			$this->db->delete("menu", array("mid" => $this->mid));
			$this->db->delete("menuitems", array("menu_id" => $this->mid));
			return ;
		}
	}
}

/* End of file menumodel.php */
/* Location: ./system/application/models/menumodel.php */